package cvs;
import java.util.HashSet;
import java.util.Hashtable;

public class DicMain {

	Hashtable<String, HashSet<String>> nonTerminals = new Hashtable<String, HashSet<String>>();

	public static void main(String[] args) {

		DicMain dicMain = new DicMain();

		dicMain.buildNonTerminal(null);

		System.out.println(dicMain.test("from la to"));
		System.out.println(dicMain.test("from san to"));
		System.out.println(dicMain.test("from san diego to"));

	}

	// string = "$city := Seoul | LA | San Francisco | San Diego";
	public void buildNonTerminal(String string) {

		HashSet<String> terminal = new HashSet<String>();

		String name = "city";
		terminal.add("la");
		terminal.add("san diego");

		nonTerminals.put(name, terminal);
	}

	public boolean test(String test) {

		String[] words = test.split(" ");

		String nameOfNonT = "city";
		int index = indexOfKey(words, 1, nameOfNonT);

		System.out.println(index);
		if (index == -1)
			return false;

		// parse next word

		return true;
	}

	// return -1 if can't find
	public int indexOfKey(String[] words, int offSet, String nameOfNonT) {

		HashSet<String> terminal = nonTerminals.get(nameOfNonT);

		String keyword = null;
		boolean isFirst = true;

		for (int i = 1; i < words.length; i++) {

			if (isFirst) {
				keyword = words[i];
				isFirst = false;
			} else
				keyword = keyword + " " + words[i];

			System.out.println("keyword=" + keyword);
			if (terminal.contains(keyword))
				return i;
		}

		// can't find
		return -1;
	}
}
